package com.beyondsoft.sabg.edu.controller;

import com.beyondsoft.sabg.edu.model.db.StudentInfo;
import com.beyondsoft.sabg.edu.model.dto.StudentDTO;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.StudentService;
import com.beyondsoft.sabg.edu.utils.PageUtils;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生信息
 *
 * @Author Mo cr
 * @Date 2023/11/14
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/list/filter")
    public SimpleResponseVO filterByPage(@RequestBody StudentDTO studentDTO) {
        Pageable pageable = PageUtils.initPage(studentDTO);
        return ResponseUtil.success().withData(studentService.findAll(studentDTO, pageable));
    }

    @PostMapping("/list/studentNumber")
    public SimpleResponseVO findAllByStudentNumber(@RequestBody List<String> studentNumber) {
        return ResponseUtil.success().withData(studentService.findAllByStudentNumber(studentNumber));
    }

    @PostMapping("/save")
    public SimpleResponseVO save(@RequestBody StudentDTO studentInfo) {
        studentService.save(new StudentInfo(), studentInfo);
        return ResponseUtil.success();
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasRole('TEACHER')||hasRole('ADMIN')")
    public SimpleResponseVO delete(@RequestBody StudentDTO studentInfo) {
        //TODO 没写批量
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(studentInfo.getId());
        studentService.deleteByIds(ids);
        return ResponseUtil.success();
    }

    @PostMapping(value = "/export-excel")
    @PreAuthorize("hasRole('TEACHER')||hasRole('ADMIN')")
    public void exportStudentInfo(@RequestBody StudentDTO studentDTO, HttpServletResponse response) throws IOException {
        studentService.exportExcel(studentDTO,response);
    }
}
